<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class admin_controller_log extends omnitrix_controller
{
	public function index_action()
	{
		$wheres = array();

		if (($user_id = otx_param_int('user_id', 0)) > 0) {
			$wheres[] = 'l.user_id = ' . $user_id;
		}

		$sql = 'SELECT l.*, u.user_name
				FROM #system_log# l
				LEFT JOIN #system_user# u ON u.user_id = l.user_id';
		extract(otx_db_list_query($sql, $wheres, 'l.log_time', 'desc'));

		$list_table = otx_html_create_table($this->_('heading_index'), 'table-list');
		$list_table->set_list_pagination($pager);
		$list_table->set_head_cell($this->_('column_user_name'), array('width' => '200'));
		$list_table->set_head_cell($this->_('column_log_time'), array('width' => '200'));
		$list_table->set_head_cell($this->_('column_log_text'));
		$list_table->set_head_cell($this->_('column_log_ip'), array('width' => '200'));

		if ($rows) {
			foreach ($rows as $row) {
				$list_table->add_body_row();
				$list_table->set_body_cell(otx_html_link($row['user_name'], array('href' => otx_router_url('index', 'profile', 'admin', array('user_id' => $row['user_id'])))));
				$list_table->set_body_cell(otx_string_format_datetime($row['log_time']));
				$list_table->set_body_cell($row['log_text']);
				$list_table->set_body_cell($row['log_ip']);
			};
		} else {
			$list_table->add_body_row();
			$list_table->set_body_cell($this->_('text_no_results'), array('colspan' => 4));
		}

		$form_wrapper = otx_html_create_wrapper(null, null, $this->_('button_filter'));
		$form_table = $form_wrapper->create_table($this->_('heading_filter'), 'table-form');

		$user_list = otx_db_fetch_pairs('SELECT user_id, user_name FROM #system_user#');
		$user_list = array_merge(array('0' => $this->_('text_all_users'), '' => $this->_('text_select_separator')), $user_list);
		$form_table->add_form_input($this->_('entry_user_id'), otx_html_form_select('user_id', $user_id, array('options' => $user_list)));

		$sort_columns['u.user_name'] = $this->_('column_user_name');
		$sort_columns['l.log_time'] = $this->_('column_log_time');
		$sort_columns['l.log_text'] = $this->_('column_log_text');
		$sort_columns['l.log_ip'] = $this->_('column_log_ip');
		$sort_orders['asc'] = $this->_('text_ascending');
		$sort_orders['desc'] = $this->_('text_descending');
		$form_table->add_form_input($this->_('entry_sort_type'), otx_html_form_select('sort_column', $pager['sort_column'], array('options' => $sort_columns)) . '&nbsp;' . otx_html_form_select('sort_order', $pager['sort_order'], array('options' => $sort_orders)));

		$form_table->add_form_input($this->_('entry_page_size'), otx_html_form_text('page_size', $pager['page_size']));

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'), true);
		otx_html_add_navtab($this->_('navtab_prune_title'), $this->_('navtab_prune_desc'), otx_router_url('prune'));
		otx_html_add_breadcrumb($this->_('breadcrumb_index'));
		otx_html_response_title($this->_('breadcrumb_index'));
	}

	public function prune_action()
	{
		if (otx_request_is_post()) {
			if (!otx_session_is_super()) {
				otx_admin_flash_error($this->_('error_super_permission'));
				otx_router_redirect('index');
			}

			$user_id = otx_param_int('user_id');
			$older_than = otx_param_int('older_than');

			$wheres[] = 'log_time < ' . ($GLOBALS['_XTIMESTAMP'] - $older_than * 86400);
			if ($user_id > 0) {
				$wheres[] = 'user_id = ' . $user_id;
				$admin_name = otx_db_fetch_one('SELECT admin_name FROM #system_user# WHERE user_id = ?', $user_id);
			} else {
				$admin_name = $this->_('text_all_users');
			}

			otx_db_delete('#system_log#', $wheres);

			$log_params = array('older_than' => $older_than, 'admin_name' => $admin_name, 'affected_rows' => otx_db_affected_rows());
			otx_admin_log_action($this->_('text_success_prune', $log_params));
			otx_admin_flash_message($this->_('text_success_prune', $log_params));
			otx_router_redirect('index');
		} else {
			$user_id = 0;
			$older_than = 30;
		}

		$form_wrapper = otx_html_create_wrapper(null, null, $this->_('button_prune'));
		$form_table = $form_wrapper->create_table($this->_('heading_prune'), 'table-form');

		$user_list = otx_db_fetch_pairs('SELECT user_id, user_name FROM #system_user#');
		$user_list = array_merge(array('0' => $this->_('text_all_users'), '' => $this->_('text_select_separator')), $user_list);
		$form_table->add_form_input($this->_('entry_user_id'), otx_html_form_select('user_id', $user_id, array('options' => $user_list)));

		$form_table->add_form_input($this->_('entry_older_than'), otx_html_form_text('older_than', $older_than));

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'));
		otx_html_add_navtab($this->_('navtab_prune_title'), $this->_('navtab_prune_desc'), otx_router_url('prune'), true);
		otx_html_add_breadcrumb($this->_('breadcrumb_index'), otx_router_url('index'));
		otx_html_add_breadcrumb($this->_('breadcrumb_prune'));
		otx_html_response_title($this->_('breadcrumb_prune'));
	}
}